package cn.chinasyq.photoquan.news.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckedTextView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;
import com.ta.utdid2.android.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

import cn.chinasyq.photoquan.R;
import cn.chinasyq.photoquan.news.bean.ArticlesInfoEntity;
import cn.chinasyq.photoquan.news.bean.CommentsEntity;
import cn.chinasyq.photoquan.news.bean.ContentEntity;
import cn.chinasyq.photoquan.bean.ShareEntity;
import cn.chinasyq.photoquan.util.BitmapUtil;
import cn.master.util.utils.ScreenUtil;
import cn.master.volley.commons.VolleyHelper;

/**
 * Created by 一搏 on 2015/11/25.
 */
public class ArticleInfoAdapter extends BaseAdapter {
    private List<Object> infoList;

    public ArticleInfoAdapter() {
        infoList = new ArrayList<>();
    }

    @Override
    public int getCount() {
        return infoList.size();
    }

    @Override
    public Object getItem(int position) {
        return infoList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Object item = getItem(position);
        if (item instanceof Title) {
            return getTitleView((Title) item, convertView, parent);
        } else if (item instanceof TimeAndAuthor) {
            return getTimeAndAuthorView((TimeAndAuthor) item, convertView, parent);
        } else if (item instanceof ContentEntity) {
            return getContentEntityView((ContentEntity) item, convertView, parent);
        } else if (item instanceof Favorites) {
            return getFavoritesView((Favorites) item, convertView, parent);
        } else if (item instanceof CommentsEntity) {
            return getCommentsEntity((CommentsEntity) item, convertView, parent);
        } else if (item instanceof Foot) {
            return getFootView((Foot) item, convertView, parent);
        } else {
            return new View(parent.getContext());
        }

    }

    private View getTitleView(Title title, View convertView, ViewGroup parent) {
        TitleViewHolder titleViewHolder;
        if (convertView == null || convertView.getTag() == null || !(convertView.getTag() instanceof TitleViewHolder)) {
            titleViewHolder = new TitleViewHolder();
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_info_title, null);
            titleViewHolder.title = (TextView) convertView.findViewById(R.id.tv_title);
            convertView.setTag(titleViewHolder);
        } else {
            titleViewHolder = (TitleViewHolder) convertView.getTag();
        }
        titleViewHolder.title.setText(title.title);
        return convertView;
    }

    class TimeAndAuthorHolder {
        TextView tv_time, tv_author;
    }

    private View getTimeAndAuthorView(TimeAndAuthor timeandauthor, View convertView, ViewGroup parent) {
        TimeAndAuthorHolder titleViewHolder;
        if (convertView == null || convertView.getTag() == null || !(convertView.getTag() instanceof TimeAndAuthorHolder)) {
            titleViewHolder = new TimeAndAuthorHolder();
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_info_time_author, null);
            titleViewHolder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
            titleViewHolder.tv_author = (TextView) convertView.findViewById(R.id.tv_author);
            convertView.setTag(titleViewHolder);
        } else {
            titleViewHolder = (TimeAndAuthorHolder) convertView.getTag();
        }
        titleViewHolder.tv_time.setText(timeandauthor.time);
        titleViewHolder.tv_author.setText(timeandauthor.author);
        return convertView;
    }

    class CommentViewHolder {
        TextView tv_name, tv_time, tv_content;
        CheckedTextView tv_like;
        NetworkImageView imageView;
    }

    private View getCommentsEntity(CommentsEntity contententity, View convertView, ViewGroup parent) {
        CommentViewHolder titleViewHolder;
        if (convertView == null || convertView.getTag() == null || !(convertView.getTag() instanceof CommentViewHolder)) {
            titleViewHolder = new CommentViewHolder();
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_info_comment, null);
            titleViewHolder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
            titleViewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            titleViewHolder.tv_content = (TextView) convertView.findViewById(R.id.tv_content);
            titleViewHolder.tv_like = (CheckedTextView) convertView.findViewById(R.id.tv_like_num);
            titleViewHolder.tv_like.setOnClickListener((View.OnClickListener) parent.getContext());
            titleViewHolder.imageView = (NetworkImageView) convertView.findViewById(R.id.iv_avatar);
            convertView.setTag(titleViewHolder);
        } else {
            titleViewHolder = (CommentViewHolder) convertView.getTag();
        }
        titleViewHolder.tv_time.setText(contententity.getTime());
        titleViewHolder.tv_name.setText(contententity.getNickname());
        titleViewHolder.tv_content.setText(contententity.getTime());
        titleViewHolder.tv_like.setText(String.valueOf(contententity.getUps()));
        titleViewHolder.tv_like.setChecked(contententity.isHas_up());
        titleViewHolder.tv_like.setTag(contententity.getUuid());
        titleViewHolder.tv_like.setEnabled(!contententity.isHas_up());
        titleViewHolder.imageView.setImageUrl(contententity.getImage(), VolleyHelper.getImageLoader());

        return convertView;
    }

    class FavoritesViewHolder {
        TextView tv_collection;
        Button btn_wchat, btn_pyq;
    }

    private View getFavoritesView(Favorites favorites, View convertView, ViewGroup parent) {
        FavoritesViewHolder titleViewHolder;
        if (convertView == null || convertView.getTag() == null || !(convertView.getTag() instanceof FavoritesViewHolder)) {
            titleViewHolder = new FavoritesViewHolder();
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_info_favorites, null);
            titleViewHolder.tv_collection = (TextView) convertView.findViewById(R.id.iv_collection);
            titleViewHolder.btn_wchat = (Button) convertView.findViewById(R.id.btn_wx);
            titleViewHolder.btn_pyq = (Button) convertView.findViewById(R.id.btn_pyq);
            titleViewHolder.btn_wchat.setOnClickListener((View.OnClickListener) parent.getContext());
            titleViewHolder.btn_pyq.setOnClickListener((View.OnClickListener) parent.getContext());
            convertView.setTag(titleViewHolder);
        } else {
            titleViewHolder = (FavoritesViewHolder) convertView.getTag();
        }
        titleViewHolder.tv_collection.setText(String.valueOf(favorites.favorites));
        titleViewHolder.btn_wchat.setEnabled(favorites.shareEntity != null);
        titleViewHolder.btn_pyq.setEnabled(favorites.shareEntity!=null);
        return convertView;
    }

    class ContentViewHolder {
        TextView tv_content;
        NetworkImageView iv_content;
    }

    private View getContentEntityView(ContentEntity commentsentity, View convertView, ViewGroup parent) {
        ContentViewHolder titleViewHolder;
        if (convertView == null || convertView.getTag() == null || !(convertView.getTag() instanceof ContentViewHolder)) {
            titleViewHolder = new ContentViewHolder();
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_info_content, null);
            titleViewHolder.tv_content = (TextView) convertView.findViewById(R.id.tv_content);
            titleViewHolder.iv_content = (NetworkImageView) convertView.findViewById(R.id.iv_content);
            convertView.setTag(titleViewHolder);
        } else {
            titleViewHolder = (ContentViewHolder) convertView.getTag();
        }
        if (ContentEntity.TYPE_IMAGE.equals(commentsentity.getType())) {
            titleViewHolder.tv_content.setVisibility(View.GONE);
            titleViewHolder.iv_content.setImageUrl(commentsentity.getUrl(), VolleyHelper.getImageLoader());
            LinearLayout.LayoutParams imageParams = (LinearLayout.LayoutParams) titleViewHolder.iv_content.getLayoutParams();
            if (BitmapUtil.getImageWHmatcher(commentsentity.getUrl())[0]>0&& BitmapUtil.getImageWHmatcher(commentsentity.getUrl())[1]>0){
                imageParams= new LinearLayout.LayoutParams(ScreenUtil.getScreenWidth(parent.getContext())-20, (ScreenUtil.getScreenWidth(parent.getContext())-20)* BitmapUtil.getImageWHmatcher(commentsentity.getUrl())[1]/BitmapUtil.getImageWHmatcher(commentsentity.getUrl())[0]);
            }else {
                imageParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            }
            titleViewHolder.iv_content.setLayoutParams(imageParams);
            titleViewHolder.iv_content.setVisibility(View.VISIBLE);
        } else if (ContentEntity.TYPE_TEXT.equals(commentsentity.getType())) {
            titleViewHolder.tv_content.setText(commentsentity.getContent());
            titleViewHolder.iv_content.setVisibility(View.GONE);
            titleViewHolder.tv_content.setVisibility(View.VISIBLE);
        }
        return convertView;
    }

    class FootViewHolder {
        TextView view;
    }

    private View getFootView(Foot foot, View convertView, ViewGroup parent) {
        if (convertView == null || convertView.getTag() == null || convertView.getTag() instanceof FootViewHolder) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_info_foot, null);
        }
        return convertView;
    }

    public void setInfoEntity(ArticlesInfoEntity infoEntity) {
        if (infoEntity == null) {
            return;
        }
        if (infoList.size() > 0) {
            infoList.clear();
        }
        if (!StringUtils.isEmpty(infoEntity.getTitle())) {
            infoList.add(new Title(infoEntity.getTitle()));
        }
        if (!StringUtils.isEmpty(infoEntity.getTime()) || !StringUtils.isEmpty(infoEntity.getAuthor())) {
            infoList.add(new TimeAndAuthor(infoEntity.getTime(), infoEntity.getAuthor()));
        }
        for (ContentEntity entity : infoEntity.getContent()) {
            infoList.add(entity);
        }
        infoList.add(new Favorites(infoEntity.getFavorites(),infoEntity.getShare()));
        for (CommentsEntity entity : infoEntity.getComments()) {
            infoList.add(entity);
        }
        if (infoEntity.getComments().size() == 0) {
            infoList.add(new Foot());
        }
        notifyDataSetChanged();
    }

    class Title {
        public Title(String title) {
            this.title = title;
        }

        String title;
    }

    class Favorites {
        public Favorites(int favorites,ShareEntity shareEntity) {
            this.shareEntity = shareEntity;
            this.favorites = favorites;
        }

        ShareEntity shareEntity;
        int favorites;
    }

    class Foot {
        boolean hasComment;
    }

    class TimeAndAuthor {
        public TimeAndAuthor(String time, String author) {
            this.time = time;
            this.author = author;
        }

        String time;
        String author;
    }

    class TitleViewHolder {
        TextView title;
    }
}
